package cn.gzsxt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.annotation.SessionScope;

import cn.gzsxt.annotation.AdminSessionUpdate;
import cn.gzsxt.annotation.TokenForm;
import cn.gzsxt.service.DictionaryService;
import cn.gzsxt.service.ModularService;
import cn.gzsxt.service.PermissionService;
import cn.gzsxt.utils.Global;
import cn.gzsxt.utils.Page;

@Controller
@SessionScope
@RequestMapping(value="/permission")
public class PermissionController {
	
private static final Logger LOGGER=LogManager.getLogger(PermissionController.class);
	
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private ModularService modularService;
	@Autowired
	private PermissionService permissionService;
	
	/**
	 * 实现权限增加
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="/addPermission")
	@TokenForm(remove=true)
	@AdminSessionUpdate
	public String  addPermission(@RequestParam Map<String, Object> entity,HttpServletRequest request){
		LOGGER.debug("增加权限"+entity);
	
		//将权限的数据插入到数据库
		try {
			Map<String, Object> permission = permissionService.addPermission(entity);
			if (permission!=null) {
				request.setAttribute("permission_add_msg", "增加权限成功");
			}else{
				request.setAttribute("permission_add_msg", "增加权限失败");
			}
		} catch (Exception e) {
			request.setAttribute("permission_add_msg", "出现未知异常，请联系管理员");
			e.printStackTrace();
		}
		
		return "forward:/permission/toPermissionAdd";
	}
	
	/**
	 * 删除权限记录
	 * @param permissionId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/deletePermissionById/{permissionId}")
	@AdminSessionUpdate
	public String deletePermissionById(@PathVariable Long permissionId,HttpServletRequest request){
		
		//通过Id删除记录
		permissionService.deletePermissionByIds(permissionId);
		
		//通过路径跳转回列表
		return "forward:/permission/toPermissionList/0";
	}
	
	/**
	 * 批量删除多条数据
	 * @return
	 */
	@RequestMapping(value="/deletePermissionByIds")
	@AdminSessionUpdate
	public String deletePermissionByIds(String[] permissionId){
		LOGGER.debug("批量删除权限"+Arrays.toString(permissionId));
		
		//通过权限编好数组删除记录
		permissionService.deletePermissionByIds((Object[])permissionId);
		
		//通过路径跳转回列表
		return "forward:/permission/toPermissionList/0";
	}
	
	/**
	 * 更新权限记录
	 * @param entity
	 * @return
	 */
	@RequestMapping(value="/editPermission")
	@AdminSessionUpdate
	public String editPermission(@RequestParam Map<String, Object> entity,HttpServletRequest request){
		LOGGER.debug("更新权限："+entity);
	
		try {
			Map<String, Object> permission = permissionService.editPermission(entity);
			if (permission!=null) {
				request.setAttribute("permission_edit_msg", "更新权限成功");
				request.setAttribute("permission", permission);
			}else{
				request.setAttribute("permission_edit_msg", "更新权限失败");
			}
		} catch (Exception e) {
			request.setAttribute("permission_edit_msg", "出现未知异常，请联系管理员");
			e.printStackTrace();
		}
		return "forward:/permission/toPermissionEdit/"+entity.get("PERMISSION_ID");
	}
	
	/**
	 * 跳转到权限增加页面
	 * @return
	 */
	@RequestMapping(value="/toPermissionAdd")
	@TokenForm(create=true)
	public String toPermissionAdd(HttpServletRequest request){
		//查询所有模块
		List<Map<String, Object>> modulars = modularService.findAllModular();
		//是否显示数据
		List<Map<String, Object>> isShow = dictionaryService.findDictionaryByTypeCode("1002");
		//权限类型数据
		List<Map<String, Object>> permissionType = dictionaryService.findDictionaryByTypeCode("1003");
		request.setAttribute("modulars", modulars);
		request.setAttribute("isShow", isShow);
		request.setAttribute("permissionType", permissionType);
		
		LOGGER.debug("跳转到权限增加页面");
		return "manager/permissionAdd";
	}
	
	/**
	 * 跳转到权限编辑页面，编辑数据必须要标识唯一的记录
	 * 所以必须要传递一个权限编号过来
	 * @return
	 */
	@RequestMapping(value="/toPermissionEdit/{permissionId}")
	public String toPermissionEdit(@PathVariable Long permissionId,HttpServletRequest request){
		LOGGER.debug("跳转到编辑权限页面"+permissionId);
		//查询所有模块
		List<Map<String, Object>> modulars = modularService.findAllModular();
		//是否显示数据
		List<Map<String, Object>> isShow = dictionaryService.findDictionaryByTypeCode("1002");
		//权限类型数据
		List<Map<String, Object>> permissionType = dictionaryService.findDictionaryByTypeCode("1003");
		request.setAttribute("modulars", modulars);
		request.setAttribute("isShow", isShow);
		request.setAttribute("permissionType", permissionType);
		//跳转到编辑页面之前，需要将要编辑的权限数据查询出来
		Map<String, Object> permission = permissionService.findPermissionById(permissionId);
		request.setAttribute("permission", permission);
		
		return "manager/permissionEdit";
	}
	
	/**
	 * 跳转到权限列表页面
	 * @param index 数据库索引=页面索引-1。
	 * @param condition 查询的条件
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/toPermissionList/{index}")
	public String toPermissionList(@PathVariable Integer index,@RequestParam Map<String, Object> condition, HttpServletRequest request) {
		LOGGER.debug("跳转到权限列表");
		if (index==null) {
			index=0;
		}
		//第一步：实现全部权限数据查询
		Page page = permissionService.findPermissionByConditionToPage(condition, index, Global.RECORD_SIZE);
		//第二步：将数据通过作用域返回到页面
		request.setAttribute("permissionPage", page);
		
		return "manager/permissionList";
	}

	

}
